package com.pphh.demo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pphh.demo.common.service.SimpleService;
import com.pphh.demo.common.service.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/test")
@Slf4j
public class DemoController {

    @Reference
    private SimpleService simpleService;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String testTrace() {
        log.info("receive a request, time = {}", new Date());

        UserEntity user = new UserEntity();
        user.setUserName("michael");
        int result = simpleService.save(user);
        log.info("result = {}", result);

        String[] strings = {"string1", "string2"};
        List<String> strList = new ArrayList<>();
        strList.add("listItem1");
        strList.add("listItem2");
        return simpleService.hello(1, strings, strList);
    }

    @RequestMapping(value = "/error", method = RequestMethod.GET)
    public String testError() throws Exception {
        log.info("receive a request for error test, time = {}", new Date());
        int result = simpleService.test(new UserEntity());
        return "this is a test for error trace, result = " + result;
    }


}
